//
//  HomeAutonomyEssentialViewController.m
//  AmountBody
//
//  Created by smm on 2017/12/16.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "HomeAutonomyEssentialViewController.h"
#import "HomeSubmitViewController.h"
#import "CustomRulerNewsView.h"
#import "CustomSketchMapTableView.h"

#import "EssentialModel.h"
#import "RulerShowView.h"
#import "XLBubbleTransition.h"

#import "CustomNewsShowMergView.h"
#import "CustomNewsAmountTableViewCell.h"

#define View_Tag 202000
#define Text_Tag 201000
#define Image_Tag 203000
@interface HomeAutonomyEssentialViewController ()<UITextFieldDelegate,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) CustomNewsShowMergView *essentialImage;
@property (strong, nonatomic) CustomSketchMapTableView *floatView;

@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) UIView * myRightView;
@property (strong, nonatomic) UIScrollView * back_RightScroll;
@property (strong, nonatomic) NSMutableArray * dataArray;
@end

@implementation HomeAutonomyEssentialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.public_headView.hidden = NO;
    self.public_headView.leftButton = [self leftButton];
    self.public_headView.titleLabel = [self titleLabel];
    self.public_headView.rightButton = [self rightButton];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadUI];
    
    
    
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (NSMutableArray *)dataArray;
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray array]init];
        NSArray * nameArr = @[@"西服",@"西裤"];
        NSArray* typeArr = @[@(AmountCellTypeForSize),@(AmountCellTypeForSize)];
        for (int i=0; i<nameArr.count; i++) {
            EssentialModel * model =[[EssentialModel alloc]init];
            model.name = [nameArr objectAtIndex:i];
            model.amountType = [[typeArr objectAtIndex:i] intValue];
            [_dataArray addObject:model];
        }
    }
    return _dataArray;
}

#pragma mark -  UI布局
- (UIButton*)rightButton
{
    CGFloat buttonW = 44;
    CGFloat buttonH = 44;
    CGFloat jianGeW = 25;
    CGRect buttonFrame = CGRectMake(SCREEN_W-buttonW-jianGeW , 30, buttonW, buttonH);
    
    UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
    [button addTarget:self action:@selector(rightButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitle:@"提交" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button sizeToFit];
    return button;
}

-(void)rightButtonPressed
{
    HomeSubmitViewController * tmpVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"HomeSubmitViewController"];
    tmpVC.hidesBottomBarWhenPushed  = YES;
    tmpVC.title = @"加入购物车";
    [self.navigationController pushViewController:tmpVC animated:YES];
}

-(void)loadUI
{
    CGFloat k_imageW = 524;
    CGFloat k_imageH = k_imageW/(524./641.);
    _back_RightScroll = [[UIScrollView alloc]init];
    _back_RightScroll.delegate = self;
    [self.view addSubview:_back_RightScroll];
    [_back_RightScroll mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(TOPNAVIGATIONBAR_H);
    }];
    _myRightView = [[UIView alloc]init];
    [_back_RightScroll addSubview:_myRightView];
    _back_RightScroll.showsVerticalScrollIndicator = NO;
    _back_RightScroll.showsHorizontalScrollIndicator = NO;
    _essentialImage = [[CustomNewsShowMergView alloc]init];
    [_myRightView addSubview:_essentialImage];

    [_essentialImage loadUIForShowMergeViewHorizontal];
    _tableView = [[UITableView alloc]init];
    [_myRightView addSubview:_tableView];
    _floatView = [[CustomSketchMapTableView alloc]init];
    [self.view addSubview:_floatView];
    _floatView.hasInfo = NO;
    [_floatView loadViewForSketchMap];
    _tableView.estimatedRowHeight = 40;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
}

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGFloat jianGeW = 45;
    CGFloat jianGeH = 25;
    CGFloat imageW = 348;
    CGFloat imageH = 192;
    CGFloat d_imageW = 280;
    CGFloat d_imageH = 320;
    [_myRightView mas_remakeConstraints:^(MASConstraintMaker *make) {
        layoutViews(^{
            make.top.left.bottom.equalTo(_back_RightScroll);
            make.right.equalTo(@(-20));
            make.width.equalTo(_back_RightScroll);
            make.height.equalTo(_back_RightScroll);
        }, ^{
            make.edges.equalTo(_back_RightScroll);
            make.width.equalTo(_back_RightScroll);
            make.height.greaterThanOrEqualTo(@0.f);
        });
        
    }];
    [_essentialImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        __block UIImage * image = [UIImage imageNamed:@"up"];
        layoutViews(^{
            image = [UIImage imageNamed:@"up"];
            make.left.equalTo(@(jianGeW));
            make.top.equalTo(@(jianGeH));
            make.width.equalTo(@(imageW));
            make.height.equalTo(@(imageH));
            [_essentialImage loadUIForShowMergeViewHorizontal];
        }, ^{
            image = [UIImage imageNamed:@"xinxifudongbeijing"];
            make.left.right.equalTo(_myRightView);
            make.top.equalTo(@(jianGeH));
            make.height.equalTo(@(160));
            [_essentialImage loadUIForShowMergeView];
        });
        CGFloat jianGe = 20;
        
        // 指定为拉伸模式，伸缩后重新赋值
        image = [image stretchImageWithInsetsTop:jianGe left:jianGe bottom:jianGe right:jianGe];
        _essentialImage.image = image;
    }];
    
    [_floatView mas_remakeConstraints:^(MASConstraintMaker *make) {
        layoutViews(^{
            make.left.equalTo(@(jianGeW+(imageW-d_imageW)/2.0));
            make.top.equalTo(@(220+TOPNAVIGATIONBAR_H));
            make.width.equalTo(@(d_imageW));
            make.bottom.equalTo(self.view).offset(-30);
        }, ^{
            make.left.equalTo(@(jianGeW));
            make.top.equalTo(@(220+TOPNAVIGATIONBAR_H));
            make.width.equalTo(@(d_imageW));
            make.height.equalTo(@(d_imageH));
        });
        
    }];
    [_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {layoutViews(^{
        _tableView.scrollEnabled = YES;
        make.top.equalTo(_myRightView);
        make.width.equalTo(_myRightView).multipliedBy(0.5).offset(20);
        make.right.bottom.equalTo(_myRightView).offset(-10);
        
    }, ^{
        _tableView.scrollEnabled = NO;
        make.left.equalTo(@(30));
        make.top.equalTo(_essentialImage.mas_bottom);
        make.right.equalTo(@(-10));
        make.height.equalTo(@(_tableView.contentSize.height));
        make.bottom.equalTo(_myRightView);
    });
        
    }];
    
}

#pragma mark - tableViewDelegate and dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EssentialModel * model = [self.dataArray objectAtIndex:indexPath.row];
    CustomNewsAmountTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CustomNewsAmountTableViewCell"];
    if (cell == nil) {
        cell = [[CustomNewsAmountTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomNewsAmountTableViewCell"];
        cell.selectionStyle  = UITableViewCellSelectionStyleNone;
        
    }
    cell.amountType = model.amountType;
    [cell setCellWithModel:model];
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}


@end
